<?php
/**
 * Account_Controller_Admin
 *  
 * Controller for actions (in admin area) of admin module
 *
 * @version 2012-10-10
 * @package account
 * @subpackage controller
 * @category Account_Controller_Admin
 */
 
 class Books_Controller_Visitor extends Controller
{

	/** 
	* Index
	* Default function that will be used upon module start-up.
    *
    * @version 2012-10-10
    * @param Request $req
    * @return view
    * @access public
    */
    public function processIndex($req)
    {
		return $this->processBooksList($req);
    }
	
	
   /**
    * Login
    * This function shows admin login form.
	* This function checks whether the "Remember Login" checkbox is checked. If true, login email will be inserted from cookies.
	* 
    * @param Request $req
    * @return View (login page)
    * @access public
    */
    public function processBooksList($req)
    {
		$dbBooks = Db::getModel('books');
		$result = array();
		$where = array();
		$sorter = array();
		$url    = new Url(); 
		$pager = new Pager($url,$req->getInt('count',5));
		$genre = $req->getInt('genre');
		$sorter = new Sorter($url);
		if($genre){
			$where = array_merge(array('genre_id'=>"=".$genre),$where);
		}
		
		$rows = $dbBooks->getBooksList($where,$pager,$sorter);
		$result['books'] = $rows;
		$pages_info = $pager->getInfo();
		$result['pager'] = array('totalPages'=>$pages_info['totalPages'],'current'=>$pages_info['current']);

		echo json_encode($result);
		exit;
    }

	public function processView($req){
		$dbBooks = Db::getModel('books');
		$result = array();
		$id = $req->getInt('id',1);

		if($id){
			$books = $dbBooks->find($id);
			foreach($books as $k=>$v){
				$books[$k] = htmlspecialchars_decode($v);
			}
			$result['books'] = $books;
			$result['status'] = "ok";
		}
		else{
			$result['error'] = array('Книга не найдена');
		}
		echo json_encode($result);
		exit;
    }
}
